; (function () {
	new Vue({
		el: '#marketAdd',
		data: {
			id: hlUrlUtil.GetQueryString('id'),
			channelCateArr: [],
			channelNameArr: [],
			processManagementArr: [],
			provinceList: [],
			cityList: [],
			areaList: [],
			companyArr: [],
			customizeArr: [],
			customizeArr0: [],
			customizeArr1: [],
			flag: false,
			showTable: true,
			detailFlag: true,
			channelCateCode: '#',
			channelNameCode: '#',
			processManagementCode: '#',
			projectName: '',
			projectNature: '',
			areaCoveredAcreage: '',
			buildingCoveredAcreage: '',
			floorCount: '',
			roomRates: '',
			standardStoreyHeight: '',
			liftCount: '',
			parkingCount: '',
			airConditionerCount: '',
			rentPrice: '',
			propertyFee: '',
			tradeZoneAvgPrice: '',
			businessForm: '',
			contactName: '',
			contactPhoneNum: '',
			buildingCompletionDate: '',
			proprietorName: '',
			proprietorProperty: '',
			developers: '',
			positionInfo: '',
			transportationFacilities: '',
			provinceId: "",//        省ID
			cityId: "",//        城市ID
			areaId: "",//        区ID
			provinceVal: '',
			cityVal: '',
			areaVal: '',
			address: '',
			projDesc: '',
			urlArr: [],
			projLegalName: '',
			companyFullName: '',
			legalPersonName: '',
			projPrOwnerName: '',
			compayId: '',
			rentAcreage: '',
			landUseRightsExpireDate: '',
			propertyAccessionDate: '',
			rightsInterestsRates: '',
			propertyProfile: '',
			state: '',
			
			tmpData: [],
			resData: []
		},
		methods: {
			//获取渠道分类
			channelCateDt() {
				hlhttp.get({
					url: 'configure/dict',
					data: { module_code: 'SCTZ', config_name: 'SCTZ_CHANNEL_CATE', parent_dict_code: '#' },
					success: (data) => {
						this.channelCateArr = data;
					}
				})
			},
			//获取渠道名称
			channelNameDt() {
				hlhttp.get({
					url: 'configure/dict',
					data: { module_code: 'SCTZ', config_name: 'SCTZ_CHANNEL_NAME', parent_dict_code: this.channelCateCode },
					success: (data) => {
						this.channelNameArr = data;
					}
				})
			},
			//选择渠道分类
			selectedCC(item) {
				this.channelCateCode = item.dictCode;
				this.channelNameArr = [];
				this.$refs.channelName.innerHTML = '请选择'
				this.channelNameDt()
			},
			//选择渠道名称
			selectedCN(item) {
				this.channelNameCode = item.dictCode;
			},
			//获取过程管理
			processManagementDt() {
				hlhttp.get({
					url: 'configure/dict',
					data: { module_code: 'SCTZ', config_name: 'SCTZ_PROCESS_MANAGER' },
					success: (data) => {
						this.processManagementArr = data;
					}
				})
			},
			//选择过程管理
			selectedPM(item) {
				this.processManagementCode = item.dictCode;
			},
			//省
			provinceData() {
				hlhttp.get({
					url: 'sub_regions',
					data: { region_id: '1' },
					success: (data) => {
						this.provinceList = data;
					}
				})
			},
			//设置省
			setProvince(data) {
				this.provinceId = data.regionId;
				this.provinceVal = data.name;
				this.cityData()
				this.areaList = [];
				this.$refs.citySel.innerHTML = '请选择城市';
				this.$refs.areaSel.innerHTML = '请选择区县';
			},
			//市
			cityData() {
				hlhttp.get({
					url: 'sub_regions',
					data: { region_id: this.provinceId },
					success: (data) => {
						this.cityList = data;
					}
				})
			},
			setCity(data) {
				this.cityId = data.regionId;
				this.cityVal = data.name;
				this.areaData()
				this.$refs.areaSel.innerHTML = '请选择区县';
			},
			//区
			areaData() {
				hlhttp.get({
					url: 'sub_regions',
					data: { region_id: this.cityId },
					success: (data) => {
						this.areaList = data;
					}
				})
			},
			setArea(data) {
				this.areaId = data.regionId;
				this.areaVal = data.name;
			},
			//上传图片
			upImg() {
				document.querySelector('.upImg').click()
			},
			//图片file
			imgFile() {
				imgupload0('.upImg', (data) => {
					this.urlArr.push({ name: data.json.name, url: data.json.url, image_id: '#' })
				})
			},
			//删除图片
			delImg(i) {
				this.urlArr.splice(i, 1)
			},
			//获取公司
			companyDt() {
				hlhttp.get({
					url: 'companys',
					success: (data) => {
						this.companyArr = data;
					}
				})
			},
			//选择公司
			selectCompany(item) {
				this.companyId = item.compayId
			},
			//自定义字段radio选择
			radioSelected(i, code) {
				this.customizeArr[i].select = code
			},
			//自定义字段checkbox选择
			checkboxSelected(i, code) {
				if (this.customizeArr[i].select.indexOf(code) == -1) {
					this.customizeArr[i].select.push(code)
					return
				}
				this.customizeArr[i].select.splice(this.customizeArr[i].select.indexOf(code), 1)
			},
			//验证必填
			validata() {
				if (!this.channelCateCode) {
					hl.toast('请选择渠道分类')
					return false;
				}
				if (!this.projectName) {
					hl.toast('请填写项目名称')
					return false;
				}
				if (!this.contactName) {
					hl.toast('请填写联系人')
					return false;
				}
				if (!this.contactPhoneNum) {
					hl.toast('请填写联系电话')
					return false;
				}
				if (!this.provinceId) {
					hl.toast('请选择省份')
					return false;
				}
				if (!this.cityId) {
					hl.toast('请选择城市')
					return false;
				}
				if (!this.areaId) {
					hl.toast('请选择区县')
					return false;
				}
				return true;
			},
			//自定义字段获取
			marketCustomizeDt(val) {
				var that = this
				return
				hlhttp.get({
					url: 'market/customize_column',
					success: (res) => {
						let arr = [...res.customizeColumns, ...res.systemColumns]
            for (var i = arr.length - 1; i >= 0; i--) {
              if (arr[i].isValid == 'false') {
                arr.splice(i,1)
              }
						}//去除多余数据（未启动，无关联）
						// if(val){
            //   arr = hlArrayUtil.removeRepeat([...val,...arr], 'configCode');
            // }
            console.log(arr)
             //若有合同详情，则合并,去重（保留合同）
            let textList = []
            let noTextList = []
            arr.map((item,index) =>{
              if(item.fieldType == 'text'){
                textList.push(item)
              }else{
                noTextList.push(item)
              }
            })
            arr = [...noTextList,...textList]
            //重新排序，将文本类型放到最后
            console.log(arr)
            arr.map(item => {
              if(!item.select){
                if (item.fieldType == 'radio' || item.fieldType == 'checkbox' || item.fieldType == 'select') {
                  item.select = [];
                  item.selectArr = item.fieldRange.split(',')
                  item.selectArrCopy = []
                  item.selectArr.map(item0 => {
                    item.selectArrCopy.push({
                      name: item0,
                      code: item0
                    })
                  })
  
                } else {
                  item.select = '';
                }
              }
						})
						
      			if(val){
              val.map(item =>{
                arr.map(single =>{
                  if(single.configCode == item.configCode){
                    single.select = item.select
                  }
                })
              })
						}
						
						that.customizeArr = arr

					}
				})
			},
			resMake (type, val) {
				// text  textarea mobile email phone  time date  dateTime
				let t0 = ['text', 'email', 'area', 'mobile', 'phone', 'time', 'date', 'dateTime', 'select', 'radio', 'checkbox'];						
				let t1 = ['file'];
				if (t0.indexOf(type) > -1) return val;
				if (t1.indexOf(type) > -1) {
					let data = []
					if (val) {
						val.map(item => {
							data.push(item.response ? item.response : item)
						})
					}
					return data
				}
			},
			
			newFieldsData () {
				let data = [];
				this.resData.map(item => {
					data.push({
						component_code: item.code,
						component_type: item.type,
						property: this.resMake(item.type, item.value)
					})
				})
				return data;
			},
			
			// 保存
			submit() {
				if (!this.validata()) return;
				if(!this.checkCustom()){
          hl.toast('请完成自定义字段')
          return
        }
				let data = {
					status: this.status,
					channel_category_code: this.channelCateCode || '#',
					channel_name_code: this.channelNameCode || '#',
					process_manager_code: this.processManagementCode || '#',
					proj_name: this.projectName || '#',
					proj_property: this.projectNature || '#',
					area_covered_acreage: this.areaCoveredAcreage ? (this.areaCoveredAcreage + '') : '#',
					building_covered_acreage: this.buildingCoveredAcreage ? (this.buildingCoveredAcreage + '') : '#',
					floor_count: this.floorCount ? (this.floorCount + '') : '#',
					room_rates: this.roomRates ? (this.roomRates + '') : '#',
					standard_storey_height: this.standardStoreyHeight ? (this.standardStoreyHeight + '') : '#',
					lift_count: this.liftCount ? (this.liftCount + '') : '#',
					parking_count: this.parkingCount ? (this.parkingCount + '') : '#',
					air_conditioner_count: this.airConditionerCount ? (this.airConditionerCount + '') : '#',
					rent_price: this.rentPrice ? (this.rentPrice + '') : '#',
					property_fee: this.propertyFee ? (this.propertyFee + '') : '#',
					trade_zone_avg_price: this.tradeZoneAvgPrice ? (this.tradeZoneAvgPrice + '') : '#',
					business_form: this.businessForm ? (this.businessForm + '') : '#',
					contact_name: this.contactName || '#',
					contact_phone_num: this.contactPhoneNum || '#',
					building_completion_date: this.buildingCompletionDate || '#',
					proprietor_name: this.proprietorName || '#',
					proprietor_property: this.proprietorProperty || '#',
					developers: this.developers || '#',
					position_info: this.positionInfo || '#',
					transportation_facilities: this.transportationFacilities || '#',
					province_id: this.provinceId || '#',
					city_id: this.cityId || '#',
					district_id: this.areaId || '#',
					province_name: this.provinceVal || '#',
					city_name: this.cityVal || '#',
					district_name: this.areaVal || '#',
					address: this.address || '#',
					address: this.address || '#',
					proj_desc: this.projDesc || '#',
					proj_legal_name: this.projLegalName || '#',
					company_full_name: this.companyFullName || '#',
					legal_person_name: this.legalPersonName || '#',
					proj_pr_owner_name: this.projPrOwnerName || '#',
					company_id: this.companyId || '#',
					company_name: this.$refs.company.innerHTML == '请选择' ? '#' : this.$refs.company.innerHTML,
					rent_acreage: this.rentAcreage ? (this.rentAcreage + '') : '#',
					land_use_rights_expire_date: this.landUseRightsExpireDate || '#',
					property_accession_date: this.propertyAccessionDate || '#',
					rights_interests_rates: this.rightsInterestsRates ? this.rightsInterestsRates + '' : '#',
					property_profile: this.propertyProfile || '#',
				}
				hlhttp.put({
					url: 'market/expansion_proj_info',
					data: {
						expansion_proj_id: this.id,
						page_customize_component_biz_content: JSON.stringify(this.newFieldsData()),
						custom_config_property:JSON.stringify(this.customizeArr),
						images_url: this.urlArr.length ? JSON.stringify(this.urlArr) : '#',
						expansion_proj_info: JSON.stringify(data)
					},
					success: (res) => {
						hlUrlUtil.goTo('projectDetail', { ddtab: true, id: this.id }, false)
					}
				})
			},
			//验证项目是否重复
			validate() {
				hlhttp.get({
					url: 'market/expansion_proj_info/search_proj_name',
					data: { proj_name: this.projectName, expansion_proj_id: this.id },
					success: (data) => {
						if (data.searchInfo == 'Y') {
							this.flag = true;
						}
						if (data.searchInfo == 'N') {
							this.submit()
						}
					}
				})
			},
			//获取详情
			detailDt() {
				hlhttp.get({
					url: 'market/expansion_proj_info',
					data: { expansion_proj_id: this.id },
					success: (data) => {
						
						this.tmpData = data.pageCustomizeComponentBizContentList ? data.pageCustomizeComponentBizContentList : []
						
						this.status = data.expansionProjInfo.status;
						this.channelCateCode = data.expansionProjInfo.channelCategoryCode;
						this.channelNameCode = data.expansionProjInfo.channelNameCode;
						this.processManagementCode = data.expansionProjInfo.processManagerCode;
						this.$refs.channelCate.innerHTML = data.expansionProjInfo.channelCategoryCode ? data.expansionProjInfo.channelCategoryCodeName : '请选择';
						this.$refs.channelName.innerHTML = data.expansionProjInfo.channelNameCode ? data.expansionProjInfo.channelNameCodeName : '请选择';
						this.$refs.processManagement.innerHTML = data.expansionProjInfo.processManagerCode ? data.expansionProjInfo.processManagerCodeName : '请选择';
						this.projectName = data.expansionProjInfo.projName;
						this.projectNature = data.expansionProjInfo.projProperty;
						this.areaCoveredAcreage = data.expansionProjInfo.areaCoveredAcreage;
						this.buildingCoveredAcreage = data.expansionProjInfo.buildingCoveredAcreage;
						this.floorCount = data.expansionProjInfo.floorCount;
						this.roomRates = data.expansionProjInfo.roomRates;
						this.standardStoreyHeight = data.expansionProjInfo.standardStoreyHeight;
						this.liftCount = data.expansionProjInfo.liftCount;
						this.parkingCount = data.expansionProjInfo.parkingCount;
						this.airConditionerCount = data.expansionProjInfo.airConditionerCount;
						this.rentPrice = data.expansionProjInfo.rentPrice;
						this.propertyFee = data.expansionProjInfo.propertyFee;
						this.tradeZoneAvgPrice = data.expansionProjInfo.tradeZoneAvgPrice;
						this.businessForm = data.expansionProjInfo.businessForm;
						this.contactName = data.expansionProjInfo.contactName;
						this.contactPhoneNum = data.expansionProjInfo.contactPhoneNum;
						this.buildingCompletionDate = hlDateUtil.formatUTCDate(data.expansionProjInfo.buildingCompletionDate, 'yyyy-MM-dd');
						this.proprietorName = data.expansionProjInfo.proprietorName;
						this.proprietorProperty = data.expansionProjInfo.proprietorProperty;
						this.developers = data.expansionProjInfo.developers;
						this.positionInfo = data.expansionProjInfo.positionInfo;
						this.transportationFacilities = data.expansionProjInfo.transportationFacilities;
						//省份区
						this.provinceId = data.expansionProjInfo.provinceId;
						this.cityId = data.expansionProjInfo.cityId;
						this.areaId = data.expansionProjInfo.districtId;
						this.$refs.provinceSel.innerHTML = data.expansionProjInfo.provinceName ? data.expansionProjInfo.provinceName : '请选择省份';
						this.$refs.citySel.innerHTML = data.expansionProjInfo.cityName ? data.expansionProjInfo.cityName : '请选择城市';
						this.$refs.areaSel.innerHTML = data.expansionProjInfo.districtName ? data.expansionProjInfo.districtName : '请选择区县';
						this.provinceVal = data.expansionProjInfo.provinceName;
						this.cityVal = data.expansionProjInfo.cityName;
						this.areaVal = data.expansionProjInfo.districtName;
						//end
						this.address = data.expansionProjInfo.address;
						this.projDesc = data.expansionProjInfo.projDesc;
						this.projLegalName = data.expansionProjInfo.projLegalName;
						this.companyFullName = data.expansionProjInfo.companyFullName;
						this.legalPersonName = data.expansionProjInfo.legalPersonName;
						this.projPrOwnerName = data.expansionProjInfo.projPrOwnerName;
						this.rentAcreage = data.expansionProjInfo.rentAcreage;
						this.landUseRightsExpireDate = hlDateUtil.formatUTCDate(data.expansionProjInfo.landUseRightsExpireDate, 'yyyy-MM-dd');
						this.propertyAccessionDate = hlDateUtil.formatUTCDate(data.expansionProjInfo.propertyAccessionDate, 'yyyy-MM-dd');
						this.rightsInterestsRates = data.expansionProjInfo.rightsInterestsRates;
						this.propertyProfile = data.expansionProjInfo.propertyProfile;
						//公司
						this.companyId = data.expansionProjInfo.companyId;
						this.$refs.company.innerHTML = data.expansionProjInfo.companyId ? data.expansionProjInfo.companyName : '请选择';
						//img
						data.imagesUrl.map(item => {
							this.urlArr.push({ name: item.name, image_id: item.id, url: item.url })
						})
						console.log(data)
						//自定义
						// this.customizeArr = JSON.parse(data.customConfigProperty);
						this.marketCustomizeDt(JSON.parse(data.customConfigProperty))

					}
				})
			},
			// 检测自定义字段填写是否符合要求
			checkCustom() {
				let flag = true
				this.resData.map(item => {
					if (item.requiredFlag == 'Y' && (!item.value || !item.value.length) && item.type != 'line') {
					  flag = false
					}
				})
				return flag
			},
		},
		mounted() {
			hlSelectUi()
			this.channelCateDt()
			this.processManagementDt()
			this.provinceData()
			this.imgFile()
			this.companyDt()
			this.detailDt()

		}
	})
}())
